package com.example.untils.pool;

import java.util.UUID;

import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;

public class StringFactory extends BasePooledObjectFactory<String> {

    /**
     * 销毁对象 <br>
     * 如果是对象是线程，线程必须已结束，如果是socket，socket必须已close，如果是文件操作，文件数据必须已flush， 且文件正常关闭。
     */
    public void destroyObject(PooledObject<String> pool) throws Exception {
        String str = pool.getObject();
        if (str != null) {
            System.out.println(str + " 销毁...");
            str = null;
        }

    }

    /**
     * 创建对象
     */
    @Override
    public String create() throws Exception {
        String i = UUID.randomUUID().toString();
        System.out.println("make " + i + " success...");
        return i;
    }

    @Override
    public PooledObject<String> wrap(String obj) {
        return new DefaultPooledObject<String>(obj);
    }

}